package com.example.sicnuaccountingapp.data.database

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import com.example.sicnuaccountingapp.data.dao.BudgetDao
import com.example.sicnuaccountingapp.data.dao.CategoryDao
import com.example.sicnuaccountingapp.data.dao.TransactionDao
import com.example.sicnuaccountingapp.data.dao.UserDao
import com.example.sicnuaccountingapp.data.entity.*

@Database(
    entities = [
        User::class,
        Category::class,
        Transaction::class,
        Budget::class
    ],
    version = 1,
    exportSchema = false
)
abstract class AppDatabase : RoomDatabase() {
    abstract fun userDao(): UserDao
    abstract fun categoryDao(): CategoryDao
    abstract fun transactionDao(): TransactionDao
    abstract fun budgetDao(): BudgetDao

    companion object {
        private const val DATABASE_NAME = "accounting_database"

        fun buildDatabase(context: Context): AppDatabase {
            return Room.databaseBuilder(
                context.applicationContext,
                AppDatabase::class.java,
                DATABASE_NAME
            )
            .addCallback(object : RoomDatabase.Callback() {
                override fun onCreate(db: SupportSQLiteDatabase) {
                    super.onCreate(db)
                    // 可以在这里添加数据库创建时的初始化操作
                }
            })
            .addMigrations(MIGRATION_1_2)
            .build()
        }

        private val MIGRATION_1_2 = object : Migration(1, 2) {
            override fun migrate(database: SupportSQLiteDatabase) {
                // 添加未来可能的数据库迁移逻辑
            }
        }
    }
} 